from flask import Flask, render_template, request, jsonify
import requests
import random

app = Flask(__name__)

API_SPACE_KEY = "ds0l3i4euxtakvw42d72myocyfvnpd72"  
FOOD_API_URL = "https://eolink.o.apispace.com/eat222/api/v1/forward/chishenme"

FALLBACK_FOODS = [
    "自选快餐", "混沌", "炸鸡汉堡", "港式烧腊","螺蛳粉","外卖",
    "火锅", "麻辣烫", "烧烤", "炒饭/粉/面","水饺","五谷鱼粉"
]

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/recommend', methods=['GET'])  
def recommend_food():
    try:
        headers = {
            "X-APISpace-Token": API_SPACE_KEY,
            "Authorization-Type": "apikey"
        }
        params = {"size": 1}
        
        response = requests.get(FOOD_API_URL, headers=headers, params=params)
        response.raise_for_status()
        print(response)
        data = response.json()
        recommendations = data.get('result', {}).get('recommendations', [])
        
        if not recommendations:
            recommendations = [random.choice(FALLBACK_FOODS)]
        
        return jsonify({
            "success": True,
            "recommendations": recommendations
        })
    
    except Exception as e:
        recommendations = [random.choice(FALLBACK_FOODS)]
        return jsonify({
            "success": False,
            "recommendations": recommendations,
            "message": str(e)
        })

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)